package dao

import (
	"context"

	"device-admin/internal/dal"
	"device-admin/internal/models"

	"192.168.1.75/go-pkg/errorx"
	"gorm.io/gen/field"
)

type TerminalConfig struct{}

// SaveConfigs
// 存在则更新，不存在则创建
func (TerminalConfig) SaveConfigs(
	ctx context.Context,
	query *dal.Query,
	records []*models.TerminalConfig,
) (successCount int64, err error) {
	tc := dal.TerminalConfig
	for _, record := range records {
		_, res := tc.WithContext(ctx).
			Assign(field.Attrs(&models.TerminalConfig{Value: record.Value})).
			Where(tc.RobotID.Eq(record.RobotID)).
			Where(tc.TerminalType.Eq(record.TerminalType)).
			Where(tc.Key.Eq(record.Key)).
			FirstOrCreate()
		if res == nil {
			successCount++
		}
	}
	return
}

// CreateConfigs 创建配置
// 删除旧的，重新创建
func (TerminalConfig) CreateConfigs(
	ctx context.Context,
	query *dal.Query,
	records []*models.TerminalConfig,
) (err error) {
	tc := dal.TerminalConfig
	// 增加新的配置
	err = tc.WithContext(ctx).Create(records...)
	if err != nil {
		return errorx.New(err.Error(), -2)
	}
	return
}

// GetConfigs 获取配置
func (TerminalConfig) GetConfigs(
	ctx context.Context,
	query *dal.Query,
	robotID string,
	terminalType string,
	key string,
) (records []*models.TerminalConfig, err error) {
	tc := dal.TerminalConfig
	tx := tc.WithContext(ctx).
		Select(tc.Key, tc.Value).
		Where(tc.RobotID.Eq(robotID)).
		Where(tc.TerminalType.Eq(terminalType))
	if key != "" {
		tx = tx.Where(tc.Key.Eq(key))
	}
	records, err = tx.Find()
	if err == nil {
		return records, nil
	}
	return nil, errorx.New(err.Error(), -2)
}

// UpdatesConfig 更新配置
func (TerminalConfig) UpdatesConfigs(
	ctx context.Context,
	query *dal.Query,
	robotID string,
	terminalType string,
	records []*models.TerminalConfig,
) (successCount int64, err error) {
	tc := dal.TerminalConfig
	for _, record := range records {
		res, _ := tc.WithContext(ctx).
			Where(tc.RobotID.Eq(robotID)).
			Where(tc.TerminalType.Eq(terminalType)).
			Where(tc.Key.Eq(record.Key)).
			UpdateSimple(tc.Value.Value(record.Value))
		if res.RowsAffected > 0 {
			successCount++
		}
	}
	return successCount, nil
}

// CreateConfigLogs 保存配置日志
func (TerminalConfig) CreateConfigLogs(
	ctx context.Context,
	query *dal.Query,
	records []*models.TerminalConfigLog,
) error {
	tcl := dal.TerminalConfigLog
	err := tcl.WithContext(ctx).Create(records...)
	if err == nil {
		return nil
	} else {
		return errorx.New(err.Error(), -2)
	}
}

// GetConfigLog 获取配置日志
func (TerminalConfig) GetConfigLogs(
	ctx context.Context,
	query *dal.Query,
	robotID string,
	terminalType string,
	startTime int64, // 开始时间
	endTime int64, // 结束时间
	page int, // 分页
	limit int, // 记录数
) ([]*models.TerminalConfigLog, error) {
	if page <= 0 {
		page = -1
	}
	if limit <= 0 {
		limit = -1
	}
	tcl := dal.TerminalConfigLog
	res, err := tcl.WithContext(ctx).
		Select(tcl.ID, tcl.Key, tcl.OldValue, tcl.NewValue, tcl.CreatedAt).
		Where(tcl.CreatedAt.Between(startTime, endTime)).
		Where(tcl.RobotID.Eq(robotID)).
		Where(tcl.TerminalType.Eq(terminalType)).
		Order(tcl.CreatedAt.Desc()).
		Offset((page - 1) * limit).
		Limit(limit).
		Find()
	if err == nil {
		return res, nil
	} else {
		return nil, errorx.New(err.Error(), -2)
	}
}

// Delete 删除配置
func (TerminalConfig) Delete(
	ctx context.Context,
	query *dal.Query,
	robotID string,
	terminalType string,
	key string,
) error {
	tcl := dal.TerminalConfig
	tx := tcl.WithContext(ctx).
		Where(tcl.RobotID.Eq(robotID)).
		Where(tcl.TerminalType.Eq(terminalType))
	if key != "" {
		tx = tx.Where(tcl.Key.Eq(key))
	}
	_, err := tx.Delete(&models.TerminalConfig{})
	return err
}
